package com.centling.smartSealForPhone.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_apply_seal_type.*
import kotlinx.android.synthetic.main.rv_apply_seal_reason_item.view.*

class ApplySealTypeActivity : TitleBarActivity() {

    private val typeList = arrayListOf<ApplyType>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_apply_seal_type)

        setTitleBarText("选择用印名称")

        rv_apply_seal_type.layoutManager = GridLayoutManager(mContext, 1)
        rv_apply_seal_type.adapter = ApplySealTypeAdapter()

        sendRequestToFetchApplySealReason()
    }

    fun sendRequestToFetchApplySealReason() {
        mProcessDialog.setTitle("正在获取用印名称").showNormal()
        getEnqueue(UrlConstants.FETCH_APPLY_SEAL_TYPE, object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String?) {
                mProcessDialog.dismiss()

                val reasonListBean = Gson().fromJson(json, TypeListBean::class.java)
                if (reasonListBean != null && reasonListBean.sealList != null) {
                    typeList.addAll(reasonListBean.sealList!!)
                } else {
                    ShowToast.shortTime("获取用印名称为空")
                    finish()
                }
                rv_apply_seal_type.adapter.notifyDataSetChanged()
            }

            override fun onFailed(reason: String?) {
                mProcessDialog.dismiss()
                ShowToast.shortTime(reason)
                finish()
            }
        })
    }

    inner class ApplySealTypeAdapter : RecyclerView.Adapter<ApplySealTypeActivity.TypeViewHolder>() {
        override fun getItemCount(): Int = typeList.size

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TypeViewHolder = TypeViewHolder(LayoutInflater.from(mContext).inflate(R.layout.rv_apply_seal_reason_item, parent, false))

        override fun onBindViewHolder(holder: TypeViewHolder, position: Int) {
            holder.itemView.tv_apply_seal_reason_name.text = typeList[position].sealName
            holder.itemView.setOnClickListener {
                val intent = Intent()
                intent.putExtra("which_apply_seal_type_id", typeList[position].sealID)
                intent.putExtra("which_apply_seal_type_name", typeList[position].sealName)
                intent.putExtra("which_apply_seal_type_need", typeList[position].sealType)
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        }

    }

    inner class TypeViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)

    inner class TypeListBean {
        var sealList: List<ApplyType>? = null
    }

    data class ApplyType(var sealID: String, val sealName: String, val sealType: String)
}
